import * as maintainService from '../../services/maintain';
import {queryAssetType} from '../../services/assetType';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryAllRoomList} from '../../services/tRoom';
import {qryRackList} from '../../services/equipment';
import {qryDepts,qryUserByDept} from '../../services/user';
import {baseUrl} from '../../config/system'
import { message } from 'antd';

export default {
  namespace: 'maintain',
  state: {
    loading: false,
    list: [],
    assetTypeTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    rackList:[],
    searchRackList:[],
    existsSerialNo:false,
    current : 1,
    currentSize : 10,
    total : 0,
    modalVisible : false,
    importModalVisible:false,
    importModalVisible2:false,
    modalType : 'create',
    newKey : '',
    newKey1 : '',
    newKey2 : '',
    groupList: [],
    attrValues:[],
    attrValuesForm:{},
    assetId:undefined,
    typeId:undefined,
    brandId:undefined,
    assetData:{},
    manageDeptTree:[],
    useDeptTree:[],
    userList:[],
    lightLoading:false,
    timeStemp:"",
    InterValObj:"",

  },
  reducers: {
    updateState(state, { payload }){
      return { ...state, ...payload };
    },
    updateInterValObj(state, { payload }){
      return { ...state, ...payload };
    },    
    showLoading(state){
        return {...state,loading:true};
    },
    hideLoading(state){
        return {...state,loading:false};
    },
    updateTimeStemp(state, { payload }){
      return { ...state, ...payload };
    },
    qryManageDeptsSuc(state, { payload: manageDeptTree }) {
      return { ...state, manageDeptTree };
    },
    qryUseDeptsSuc(state, { payload: useDeptTree }) {
      return { ...state, useDeptTree };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    checkSerialNoSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryModelListSuc(state, { payload: assetModelList }) {
      return { ...state, assetModelList };
    },
    qryRackListSuc(state, { payload: rackList }) {
      return { ...state, rackList };
    },
    searchRackListSuc(state, { payload: searchRackList }) {
      return { ...state, searchRackList };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
    showExcelModal (state) {
        return { ...state,  newKey1 : new Date().getTime() + '' , importModalVisible: true }
    },
    hiddenExcelModal (state) {
      return { ...state, importModalVisible: false, newKey1 : new Date().getTime() + '' }
    },
    showDownLoadExcelModal (state) {
        return { ...state,  newKey2 : new Date().getTime() + '' , importModalVisible2: true }
    },
    hiddenDownLoadExcelModal (state) {
      return { ...state, importModalVisible2: false, newKey2 : new Date().getTime() + '' }
    },

  },
  effects: {
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },
    *queryList({ payload },{ call, put }) {
      yield put({ type: 'showLoading' });
      const { body } = yield call(maintainService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
      yield put({ type: 'hideLoading' });
    },
    *closeExcelModal({ payload },{ call, put }) {
      yield put({ type: 'hiddenExcelModal' });
      yield put({ type: 'queryList', payload:{ 'status':'' } });
    },
    *queryListLight({ search },{ call, put }) {
      yield put({ type: 'updateState',payload: {lightLoading:false} });
      const { body } = yield call(maintainService.queryList,search);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
      yield put({ type: 'hideLoading' });
    },

    *downLoadEXL({ payload },{ call, put }) {
      const { body } = yield call(maintainService.downLoadEXL,payload);
    },

    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'queryModelListSuc', payload: body.data });
    },
    //点亮
    *onLight({ payload,search },{ call, put }) {  
      yield put({ type: 'updateState', payload: {lightLoading:true} });
      const { body } = yield call(maintainService.onLight,payload);
      yield put({ type: 'updateTimeStemp', payload: {timeStemp:body.data} });   
    },

     //取消点亮
    *cancleLight({ payload,search },{ call, put }) {      
      yield put({ type: 'updateState', payload: {lightLoading:true} });
      const { body } = yield call(maintainService.cancleLight,payload);
      yield put({ type: 'updateTimeStemp', payload: {timeStemp:body.data} });  
    },

    *qryRackList({ payload },{ call, put }) {
      const { body }  = yield call(qryRackList,payload);
      yield put({ type: 'qryRackListSuc', payload:body.data});
    },

    *qryManageDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'qryManageDeptsSuc', payload: body.data });
    },

    *qryUseDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'qryUseDeptsSuc', payload: body.data });
    },

    *qryUserByDept({ payload },{ call, put }) {    
      const { body }  = yield call(qryUserByDept,payload);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },

    *qrySearchRackList({ payload },{ call, put }) {
      if(payload){
        const { body }  = yield call(qryRackList,payload);
        yield put({ type: 'searchRackListSuc', payload:body.data});
      }else{
        yield put({ type: 'searchRackListSuc', payload:[]});
      }     
    },

    *qryById({ payload },{ call, put }){
      const { body } = yield call(maintainService.qryById,payload.assetId);
      if(body){
        yield put({
          type: 'showModal',
          payload: {
            assetData:body.data,
            assetId: payload.assetId,
            typeId:payload.typeId,
            brandId:payload.brandId,
            modalType: 'edit',
            title: '资产配置',
          },
      });
       yield put({type: 'qryRackList',payload:payload.roomId});
       yield put({type: 'qryManageDepts'});
       yield put({type: 'qryUseDepts'});
       yield put({type: 'qryUserByDept',payload:{deptId:payload.useBranchId }});
       yield put({type: 'queryAssetModel',payload:{typeId:payload.typeId,brandId:payload.brandId}});
      }
    },
    
    *qryAttrValue({ payload },{ call, put }){
      const { body } = yield call(maintainService.qryAttrValue,payload);
      if(body){
        var dat = {};
        body.attrValues?body.attrValues.map(d=>{
          dat[d.attrIdStr] = d.value;
        }):null
        yield put({
          type: 'updateState',
          payload: {
            groupList: body.data,
            attrValues:body.attrValues,
            attrValuesForm: dat,
          },
        });      
      }
    },

    *edit({ payload, resetFields,search },{ call, put }){
      const { body } = yield call(maintainService.edit,payload);
      if(body.httpCode==200){
        yield put({ type: 'queryList', payload:search });
        yield put({ type: 'hideModal' });
        resetFields()
      }
    },
    *checkSerialNo({ payload},{ call, put }){
      const { body } = yield call(maintainService.checkSerialNo,payload);
      if(body.data==1){
         yield put({ type: 'checkSerialNoSuc',payload: { existsSerialNo:true}});
      }
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/asset/maintain') {
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryAssetBrand' ,payload: {  }, });
          dispatch({ type: 'queryRoom' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});     
        }
      });
    },
  },
};
